﻿using System.Linq;
using System.Windows.Media;
using System.Xml.Linq;

namespace CharacterModel
{
    public static class SvgToFeatureConverter
    {
        public static Layer LayerFromGroup(XElement element)
        {
            var layer = new Layer();

            var layerElement = element.Attributes().FirstOrDefault(attr => attr.Name.LocalName == "label");
            if (layerElement == null) return null;

            layer.LayerType = LayerType.FromName(layerElement.Value);
            if (layer.LayerType == null) return null;

            var newElements = element
                .Elements()
                .Where(elem => elem.Name.LocalName == "path")
                .Select(PathFromElement);

            foreach (var elem in newElements)
                layer.Paths.Add(elem);

            return layer;
        }

        public static LayerSvgPath PathFromElement(XElement element)
        {
            var path = new LayerSvgPath();
            path.Path = PathGeometry.CreateFromGeometry(Geometry.Parse(element.Attribute("d").Value));
            return path;
        }
    }
}